﻿using Microsoft.AspNetCore.Mvc;
using Logistics.Setup;
using System.Linq;
using Webdiyer.AspNetCore;

namespace Logistics.Web.Controllers
{
    public class SetupController : Controller
    {
        private readonly IRoleAppService _RoleAppService;
        public SetupController(IRoleAppService RoleAppService)
        {
            _RoleAppService = RoleAppService;
        }

        /// <summary>
        /// 显示页面
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <returns></returns>
        public IActionResult Index()
        {
            return View();
        }

        /// <summary>
        /// 显示方法
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <returns></returns>
        public object Indexme(int page = 1, int limit = 3, string name = "")
        {

            var getFoodListDto = new GetRoleListDto
            {
                Name = name,
                SkipCount = (page - 1) * limit,
                MaxResultCount = limit,
                Sorting = "Id asc"
            };

            var foods = _RoleAppService.GetRoleAsync(getFoodListDto).Result;
            var count = foods.TotalCount;
            return Ok(new { data = foods.Items, count = count, code = "0", msg = "" });
        }


        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="pageIndex"></param>
        public int DeleteIdas(int id)
        {
            var result = _RoleAppService.DeleteAsync(id);
            return result.Id;
        }
        /// <summary>
        /// 批量删除
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <returns></returns>
        //[HttpDelete]
        public int Pdelete(string ids)
        {
            var i = _RoleAppService.DeletePdeleteAsync(ids);
            return i;
        }

        /// <summary>
        /// 添加
        /// </summary>
        /// <returns></returns>
        public IActionResult AddEf(CreateRoleDto input)
        {
            var list = _RoleAppService.CreateAddAsync(input);
            if (list != null)
            {
                return Content("<script>alert('添加成功');location.href='/Setup/Index';</script>", "text/html", System.Text.Encoding.UTF8);
            }
            return Ok(list);
        }

        /// <summary>
        /// 添加页面
        /// </summary>
        /// <returns></returns>
        public IActionResult AddRole()
        {
            return View();
        }

        /// <summary>
        /// 编辑页面
        /// </summary>
        /// <returns></returns>
        public IActionResult FanTian(int id)
        {
            ViewBag.id = id;
            return View();
        }

        /// <summary>
        /// 反填方法
        /// </summary>
        /// <returns></returns>
        public JsonResult GetBack(int id)
        {
            var result = _RoleAppService.GetAsync(id).Result;
            return Json(result);
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <returns></returns>
        public IActionResult updaterole(int id, UpdateRoleDto input)
        {
            var list = _RoleAppService.UpdateAsync(id, input);
            if (list != null)
            {
                return Content("<script>alert('修改成功');location.href='/Setup/Index';</script>", "text/html", System.Text.Encoding.UTF8);
            }
            return Ok(list);
        }
        public IActionResult RoleQx(int id)
        {
            ViewBag.id = id;
            return View();
        }

        /// <summary>
        /// 获取所有权限信息，用于绑定树形控件
        /// </summary>
        /// <returns></returns>
        public JsonResult GetAllPower()
        {
            var power = _RoleAppService.GetJurisdictions();
            return Json(power);
        }

        public JsonResult PostRolePower(int id, string ids)
        {
            int i = _RoleAppService.PostRolePower(id, ids);
            return Json(i);
        }

        /// <summary>
        /// 获取角色权限
        /// </summary>
        /// <returns></returns>
        public JsonResult GetRolePower(int id)
        {
            var list = _RoleAppService.GetRolePower(id);
            return Json(list);
        }
    }
}
